#include <iostream>
#include <cstring>
#include <queue>

using namespace std;

const int N = 110, INF = 0x3f3f3f3f;

typedef pair<int, int> PII;

int a[N][N];
int st[N][N];
int n, m;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

int calc(int x, int y)
{
	int ret = 0;
	for(int i = -1; i <= 1; i++)
	{
		for(int j = -1; j <= 1; j++)
		{
			int nx = x + i, ny = y + j;
			if(nx >= 1 && ny >= 1 && nx <= n && ny <= m)
			{
				if(a[nx][ny] == 1) ret++;
			}
		}
	}
	return ret;
}

void bfs()
{
	queue<PII> q;
	q.push({1, 1});
	if(a[1][1] == 1) st[1][1] = 9;
	else st[1][1] = calc(1, 1);
	while(q.size())
	{
		auto t = q.front(); q.pop();
		int cx = t.first, cy = t.second;
		for(int k = 0; k < 4; k++)
		{
			int nx = cx + dx[k], ny = cy + dy[k];
			if(nx >= 1 && ny >= 1 && nx <= n && ny <= m)
			{
				if(st[nx][ny] != INF) continue;
				if(a[nx][ny] == 1) st[nx][ny] = 9;
				else st[nx][ny] = calc(nx, ny);
				q.push({nx, ny});
			}
		}
	}
	
}

int main()
{
	cin >> n >> m;
	
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= m; j++)
		{
			cin >> a[i][j];
		}
	}
	
	memset(st, 0x3f, sizeof st);
	
	bfs();
	
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= m; j++)
		{
			cout << st[i][j] << " ";
		}
		cout << endl;
	}
	
	return 0;
}
